$(function () {
    $("#loginBtn").bind("click", loginSubmit);

    $(document).keypress(function (e) {
        // 回车键事件
        if (e.which == 13) {
            loginSubmit();
        }
    });

    function loginSubmit() {
        var name = $("#userName").val();
        var pwd = $("#passWord").val();

        if (name == "" || pwd == "") {
            alert("用户名和密码不能为空！");
            return false;
        }

        $.ajax({
            type: "post",
            url: "/login/submit",
            data: {userName: name, passWord: pwd},
            dataType: "json",
            success: function (flag) {
                if (flag) {
                    var redirectUrl = getUrlParam("url");
                    redirectUrl = redirectUrl == null ? "/works/list" : redirectUrl;
                    window.location.href = redirectUrl;
                } else {
                    alert("用户名或者密码不正确!");
                }
            }
        });
    }

    function getUrlParam(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
        var r = window.location.search.substr(1).match(reg);  //匹配目标参数
        if (r != null) return unescape(r[2]);
        return null; //返回参数值
    }
});